﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager

Public Class CategoriaDAL : Inherits DBManager

    Public Function NuevoCategoria(ByVal categoria As Categoria) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertCategoria")
        comando.Parameters.Add("@codigoCategoria", SqlDbType.Char, 5).Value = categoria.codigoCategoria
        comando.Parameters.Add("@nombreCategoria", SqlDbType.VarChar, 40).Value = categoria.nombreCategoria
        comando.Parameters.Add("@descripcionCategoria", SqlDbType.VarChar, 200).Value = categoria.descripcionCategoria
        comando.Parameters("@codigoCategoria").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)
        categoria.codigoCategoria = comando.Parameters("@codigoCategoria").Value
        Return categoria.codigoCategoria
    End Function

    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of Categoria)
        Dim Lista As New List(Of Categoria)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim categoria As New Categoria(reader.GetString(0), reader.GetString(1), reader.GetString(2))
                Lista.Add(categoria)
                categoria = Nothing
            End While
        End Using
        Return Lista
    End Function

    Public Function GetAllFromCategoria() As List(Of Categoria)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromCategoria")
        Return SelectObjectFactory(comando)
    End Function

    Public Function SearchFromCategoria(ByVal texto As String) As List(Of Categoria)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchCategoria")
        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto
        Return SelectObjectFactory(comando)
    End Function

    Public Function DeleteCategoria(ByVal codigoCategoria As String) As Boolean
        Dim correcto As Boolean = False
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteCategoria")
        comando.Parameters.Add("@codigoCategoria", SqlDbType.Char, 5).Value = codigoCategoria

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If
        Return correcto
    End Function


End Class
